package com.cari.oa.service;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cari.oa.dao.BaseDao;
import com.cari.oa.dao.MarketProjectDao;
import com.cari.oa.domain.MarketProjectEntity;
import com.cari.oa.util.StrUtils;

@Service
@Transactional(rollbackFor = Exception.class)
public class MarketProjectService extends BaseService<MarketProjectEntity> {
	
	@Resource
	private MarketProjectDao marketProjectDao;
	
	/**
     * 分页查询系统用户列表
     * @param userEnname 系统用户英文名
     * @param userCnname 系统用户中文名
     * @param status 系统用户状态
     * @param page 当前页
     * @param size 每页条数
     * @return 分页列表
     */
    @Transactional(readOnly = true)
    public List<MarketProjectEntity> getPage(String name, String province, String applyUserCN, String applyUser, Date applyTime, String status, Integer sort, int page, int size) {
    	if(StrUtils.isNotEmpty(province))
    		province = "'" + province + "'";
    	return marketProjectDao.getPage(name, province, applyUserCN, applyUser, applyTime, status, sort, page, size);
    }
    
    @Transactional(readOnly = true)
    public List<MarketProjectEntity> getPage(String name, Set<String> provinces, String applyUserCN, String applyUser, Date applyTime, String status, Integer sort, int page, int size) {
    	String province = "";
    	if(provinces.size() > 0){
	   	 int i = 0;
	   	 for(String str : provinces){
	   		 i++;
	   		 if(i == provinces.size()){
	   			 province = province + "'" + str + "'";
	   	 }else{
	   		 province = province + "'" + str + "'" +",";
	   		 }
	   	 }
    	}else{
    		province = "' '";
    	}
    	return marketProjectDao.getPage(name, province, applyUserCN, applyUser, applyTime, status, sort, page, size);
    }
    
    /**
     * 系统用户查询记录条数
     * @param userEnname 系统用户英文名
     * @param userCnname 系统用户中文名
     * @param status 系统用户状态
     * @return 查询记录条数
     */
    @Transactional(readOnly = true)
    public long getCount(String name, String province, String applyUserCN, String applyUser, Date applyTime, String status) {
    	if(StrUtils.isNotEmpty(province))
    		province = "'" + province + "'";
    	return marketProjectDao.getCount(name, province, applyUserCN, applyUser, applyTime, status);
    }
    
    @Transactional(readOnly = true)
    public long getCount(String name, Set<String> provinces, String applyUserCN, String applyUser, Date applyTime, String status) {
    	String province = "";
    	if(provinces.size() > 0){
	   	 int i = 0;
	   	 for(String str : provinces){
	   		 i++;
	   		 if(i == provinces.size()){
	   			 province = province + "'" + str + "'";
	   	 }else{
	   		 province = province + "'" + str + "'" +",";
	   		 }
	   	 }
    	}else{
    		province = "' '";
    	}
    	return marketProjectDao.getCount(name, province, applyUserCN, applyUser, applyTime, status);
    }

	@Override
	public BaseDao<MarketProjectEntity> getDao() {
		return marketProjectDao;
	}
	
	@Transactional(readOnly = true)
    public List<MarketProjectEntity> search(String name, String province, String applyUserCN, String applyUser, Date applyTime, String status, Integer sort) {
    	if(StrUtils.isNotEmpty(province))
    		province = "'" + province + "'";
    	return marketProjectDao.search(name, province, applyUserCN, applyUser, applyTime, status, sort);
    }
    
    @Transactional(readOnly = true)
    public List<MarketProjectEntity> search(String name, Set<String> provinces, String applyUserCN, String applyUser, Date applyTime, String status, Integer sort) {
    	String province = "";
    	if(provinces.size() > 0){
	   	 int i = 0;
	   	 for(String str : provinces){
	   		 i++;
	   		 if(i == provinces.size()){
	   			 province = province + "'" + str + "'";
	   	 }else{
	   		 province = province + "'" + str + "'" +",";
	   		 }
	   	 }
    	}else{
    		province = "' '";
    	}
    	return marketProjectDao.search(name, province, applyUserCN, applyUser, applyTime, status, sort);
    }

	/**
	* 获取申请人的所有项目
	*@param username
	*@return
	*@author hjx
	*@date 2013-5-7下午2:58:35
	*/
	public List<MarketProjectEntity> getProjectByApplyUser(String username) {
		return marketProjectDao.getProjectByApplyUser(username);
	}

}
